package com.realgodo.site.web.admin.info;

import java.util.Date;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springside.modules.orm.Page;

import com.realgodo.site.entity.site.Info;
import com.realgodo.site.service.site.InfoManager;
import com.realgodo.site.web.admin.AdminDefaultController;

@Scope("prototype")
@Results( { @Result(name = "list", type = "redirect", location = "${base}/admin/info/") })
public class IndexController extends AdminDefaultController {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7827779684700392216L;
	@Autowired
	private InfoManager infoManager;
	private Page<Info> infoPage = new Page<Info>(PAGE_SIZE);
	private Info info;

	public String execute() throws Exception {
		infoPage
				.setPageNo(infoManager.isParamNotNull(getParamMap(), Page.PAGE) ? Integer
						.valueOf((String) infoManager.getParamValue(
								getParamMap(), Page.PAGE))
						: 1);// 查询页数
		this.infoPage = infoManager.find(PAGE_SIZE, super.getParamMap());
		super.setPageInfo(infoPage);
		return SUCCESS;
	}

	public String add() throws Exception {
		info = new Info();
		return "add";
	}

	public String edit() throws Exception {
		info = infoManager.get(id);
		return "add";
	}

	public String save() {
		if (info.getId() != null && !Long.valueOf(-1).equals(info.getId())) {
			String title = info.getTitle();
			info = infoManager.get(info.getId());
			info.setTitle(title);
		}
		infoManager.save(info);
		return "list";
	}

	public String delete() {
		infoManager.delete(id);
		return "list";
	}

	public String pub() {
		info = infoManager.get(id);
		info.setStatus(Info.STATUS_PUB);
		info.setPubDate(new Date());
		infoManager.save(info);
		return "list";
	}

	// -------------get/set------------//
	public Info getInfo() {
		return info;
	}

	public void setInfo(Info info) {
		this.info = info;
	}

	public Page<Info> getInfoPage() {
		return infoPage;
	}
}
